#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""A control that contains other controls."""

namespace Grawlix.Application.Gui
import Grawlix.Meta
import C5
import System

[Disposable]
abstract transient class CompoundControl(Control):
"""A control that houses multiple child controls."""
	
	protected def constructor():
	"""Default constructor."""
		pass
	
	def CheckForCycle([required] child as Control):
	"""
	Checks to see if adding a child would result in a cycle.
	Param child: The child to check.
	Returns: True if a cycle would occur, else false.
	"""
		check = self
		while check is not null:
			if child == check:
				return true
			check = check.Parent
		return false
	
	protected def AddChild([required] child as Control):
	"""
	Add a child to this control.
	Param child: The child control to add.
	Returns: true if add succeeds, else false.
	"""
		if _children.Contains(child) or CheckForCycle(child):
			return false
		if child.Parent is not null:
			check = child.Parent.RemoveChild(child)
			assert check
		child.Parent = self
		_children.Add(child)
		return true
	
	protected def RemoveChild([required] child as Control):
	"""
	Remove a child from this control.
	Param child: The child control to remove.
	Raises ArgumentException: If the child is not in this control.
	Returns: true if remove succeeds (the child was in the table), else false.
	"""
		if not _children.Contains(child):
			return false
		assert child.Parent is self
		child.Parent = null
		_children.Remove(child)
		return true
	
	Children:
	"""The children of this control."""
		get:
			return GuardedCollection[of Control](_children)
	
	private _children = HashSet[of Control]()
	"""The children of this control."""
	
	
	
	
	
	
	
	